package com.ksm.version.service.impl;

import com.ksm.common.config.RuoYiConfig;
import com.ksm.common.core.domain.entity.SysUser;
import com.ksm.common.core.domain.model.LoginUser;
import com.ksm.common.exception.BaseException;
import com.ksm.common.utils.ServletUtils;
import com.ksm.common.utils.file.FileUploadUtils;
import com.ksm.framework.web.service.TokenService;
import com.ksm.version.domain.LocalStorage;
import com.ksm.version.domain.SysVersionManage;
import com.ksm.version.domain.vo.FileVo;
import com.ksm.version.mapper.FileUploadMapper;
import com.ksm.version.mapper.ProjectMapper;
import com.ksm.version.service.FileService;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@Service
public class FileServiceImpl implements FileService {


    @Autowired(required = false)
    private FileUploadMapper mapper;

    @Autowired
    private TokenService tokenService;


    @Autowired(required = false)
    private ProjectMapper projectMapper;

    @Override
    @Transactional(isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public Integer add(MultipartFile file) throws IOException {
        String filename = file.getOriginalFilename();
        if (!file.isEmpty()) {
            String path = RuoYiConfig.getUploadPath() + FileUploadUtils.upload(RuoYiConfig.getUploadPath(), file);
            LocalStorage storage = new LocalStorage();
            storage.setAbbreviation(filename.substring(0, filename.indexOf(".")));
            storage.setPath(path);
            storage.setSuffix(FileUploadUtils.getExtensionName(filename));
            storage.setSize(FileUploadUtils.getSize(file.getSize()));
            storage.setType(FileUploadUtils.getFileType(FileUploadUtils.getExtensionName(filename)));
            storage.setCreateBy(tokenService.getLoginUser(ServletUtils.getRequest()).getUser().getUserName());
            File file1 = new File(path);
            if (file1.exists()) {
                String name = file1.getName();
                storage.setRealName(name);
                SysVersionManage manage = new SysVersionManage();
                manage.setPackageName(filename.substring(0, filename.indexOf(".")));
                manage.setFileName(name);
                manage.setUrl(path);
                projectMapper.findOneByFileName(manage);
            }
            return mapper.addFile(storage);
        } else {
            throw new BaseException("文件异常");
        }

    }

    @Override
    @Transactional(isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public Integer updatePackage(FileVo fileVo) {
        SysVersionManage manage = new SysVersionManage();
        manage.setCurrentVersion(fileVo.getVersion());
        manage.setDeployId(fileVo.getVersionId());
        return projectMapper.findOneByDeployId(manage);
    }

}
